print("加载fn.lua！")
module(..., package.seeall)
function create_ans_str(cmd_id, payload)
    local ans_str = string.fromHex("55aa00") .. string.char(cmd_id)
    if not payload or payload == "" then
        ans_str = ans_str .. string.fromHex("0000")
    else
        ans_str = ans_str .. string.char(math.floor(#payload / 256)) .. string.char(#payload % 256) .. payload
    end
    local sum = 0
    for i = 1, #ans_str do
        sum = sum + string.byte(ans_str, i)
    end
    ans_str = ans_str .. string.char(sum % 256)
    return ans_str
end

function create_ans_str_bigfile(cmd_id, file_size, send_count, payload)
    local ans_str = string.fromHex("55aa00") .. string.char(cmd_id)
    if not payload or payload == "" then
        ans_str = ans_str .. string.fromHex("0000")
    else
        ans_str =
            ans_str ..
            string.char(math.floor(file_size / 256)) ..
                string.char(file_size % 256) .. string.char(send_count % 256) .. string.char(#payload % 256) .. payload
    end
    local sum = 0
    for i = 1, #ans_str do
        sum = sum + string.byte(ans_str, i)
    end
    ans_str = ans_str .. string.char(sum % 256)
    return ans_str
end

function create_ans_str_rrpc(cmd_id, payload, msg_id)
    local ans_str = string.fromHex("55aa00") .. string.char(cmd_id)
    if not payload or payload == "" then
        ans_str = ans_str .. string.fromHex("0000")
    else
        ans_str = ans_str .. msg_id .. string.char(math.floor(#payload / 256)) .. string.char(#payload % 256) .. payload
    end
    local sum = 0
    for i = 1, #ans_str do
        sum = sum + string.byte(ans_str, i)
    end
    ans_str = ans_str .. string.char(sum % 256)
    return ans_str
end

function uart_answer(cmd_id, payload)
    print("串口应答！")
    local ans = ""
    ans = fn.create_ans_str(cmd_id, payload)
    print("组合成的应答字串为", string.toHex(ans))
    uart.write(_G.UART_ID, ans)
end

--为大文件分配发送做的应答，多了几个参数 55 AA 00 13 +文件size(2字节) + 当前传输包数量(1字节) + 当前传输包长度 + 校验(ADD8)
function uart_answer_bigfile(cmd_id, file_size, send_count, payload)
    print("大文件包组合！")
    local ans = ""
    ans = fn.create_ans_str_bigfile(cmd_id, file_size, send_count, payload)
    print("组合成的应答字串为", string.toHex(ans), "！")
    uart.write(_G.UART_ID, ans)
end
--RRPC消息处理
function uart_answer_rrpc(cmd_id, payload, msg_id)
    ans = fn.create_ans_str_rrpc(cmd_id, payload, msg_id)
    print("组合成的应答字串为", string.toHex(ans), "！")
    uart.write(_G.UART_ID, ans)
end

function print_table(t)
    local print_r_cache = {}
    local function sub_print_r(t, indent)
        if (print_r_cache[tostring(t)]) then
            print(indent .. "*" .. tostring(t))
        else
            print_r_cache[tostring(t)] = true
            if (type(t) == "table") then
                for pos, val in pairs(t) do
                    if (type(val) == "table") then
                        print(indent .. "[" .. pos .. "] => " .. tostring(t) .. " {")
                        sub_print_r(val, indent .. string.rep(" ", string.len(pos) + 8))
                        print(indent .. string.rep(" ", string.len(pos) + 6) .. "}")
                    elseif (type(val) == "string") then
                        print(indent .. "[" .. pos .. '] => "' .. val .. '"')
                    else
                        print(indent .. "[" .. pos .. "] => " .. tostring(val))
                    end
                end
            else
                print(indent .. tostring(t))
            end
        end
    end
    if (type(t) == "table") then
        print(tostring(t) .. " {")
        sub_print_r(t, "  ")
        print("}")
    else
        sub_print_r(t, "  ")
    end
    print()
end

function bin2hex(s)
    s =
        string.gsub(
        s,
        "(.)",
        function(x)
            return string.format("%02X ", string.byte(x))
        end
    )
    return s
end

function Split(szFullString, szSeparator)
    local nFindStartIndex = 1
    local nSplitIndex = 1
    local nSplitArray = {}
    while true do
        local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
        if not nFindLastIndex then
            nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
            break
        end
        nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
        nFindStartIndex = nFindLastIndex + string.len(szSeparator)
        nSplitIndex = nSplitIndex + 1
    end
    return nSplitArray
end
